﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AuctionDataLayer;

namespace Auction_Intranet.Models
{
    public class BidModel
    {
        DataRepository m_rep = new DataRepository();
        public BidModel()
        {

            var username = (HttpContext.Current.User.Identity.Name).Split('\\')[1];
            var user = m_rep.GetUserByUsername(username);

            userName = username;

            availableCredit = user.Balance;

            groups = new List<SelectListItem>();            

            var myGroups = m_rep.GetGroupsByUserId(user.Id);
            if (myGroups.Count() == 0)
            {
                groups.Add(new SelectListItem { Text = "No groups", Value = "", Selected = true });
            }
            else 
            {
                groups.Add(new SelectListItem { Text = "-Select-", Value = "", Selected = true });
            }     
            foreach (var item in myGroups)
            {
                groups.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() });
            }

            groupsCredit = myGroups;

            rooms = new List<SelectListItem>();
            rooms.Add(new SelectListItem { Text = "Any", Value = "-1", Selected = true });

            var roomAvailable = m_rep.GetAllRooms();
            foreach (var item in roomAvailable)
            {
                rooms.Add(new SelectListItem { Text = item.Name, Value = item.Id.ToString() });
            }

            var timesAvailable = m_rep.GetAvailableBidTimes();
            startTimes = new List<SelectListItem>();
            startTimes.Add(new SelectListItem { Text = "-Select-", Value = "", Selected = true });
            foreach (var item in timesAvailable)
            {
                startTimes.Add(new SelectListItem { Text = item.Startime.ToShortTimeString(), Value = item.Startime.Hour.ToString() + ":" + item.Startime.Minute.ToString() });              
            }

            endTimes = new List<SelectListItem>();
            endTimes.Add(new SelectListItem { Text = "-Select-", Value = "", Selected = true });
            foreach (var item in timesAvailable)
            {
                endTimes.Add(new SelectListItem { Text = item.Endtime.ToShortTimeString(), Value = item.Endtime.Hour.ToString() + ":" + item.Endtime.Minute.ToString() });              
            }                        

            bidFor = new List<SelectListItem>();
            bidFor.Add(new SelectListItem { Text = username, Value = "0", Selected = true });
            bidFor.Add(new SelectListItem { Text = "Group", Value = "1" });

            roomsAvailable = m_rep.GetAllRooms();

            success = 0;
        }
        public string userName { get; set; }

        [DataType(DataType.Date)]
        [Display(Name = "Day of bid")]
        [Required(ErrorMessage="Day of bid field is requered.")]
        public DateTime date { get; set; }

        public DateTime dateFinal { get; set; }

        [Display(Name = "Start time")]
        [Required(ErrorMessage = "Start time field is requered.")]
        public string startTime { get; set; }

        [Display(Name = "End time")]
        [Required(ErrorMessage = "End time field is requered.")]
        public string endTime { get; set; }

        //[Required(ErrorMessage = "Field required")]
        public string groupId { get; set; }

        [Required(ErrorMessage = "Room field is requered")]
        public string roomId { get; set; }

        public int availableCredit { get; set; }

        //[Required(ErrorMessage = "Field required")]
        [Display(Name = "Bidding amount")]
        //[RegularExpression(@"[-+]?[0-9]*\.?[0-9]?[0-9]", ErrorMessage = "Number required.")]
        //[Range(0, 9999.99, ErrorMessage = "Value must be between 0 - 9,999.99")]
        //public decimal? OverFlowCapacity { get; set; }
        [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Positive whole number is required.")]  
        public int biddingAmount { get; set; }

        public int state { get; set; }

        public List<SelectListItem> groups { get; set; }

        public List<SelectListItem> rooms { get; set; }

        public DateTime startTimesStart { get; set; }
        public List<SelectListItem> startTimes { get; set; }

        public DateTime endTimesStart  { get; set; }
        public List<SelectListItem> endTimes { get; set; }

        [Required(ErrorMessage = "Field required")]
        public string bidForId { get; set; }

        public List<SelectListItem> bidFor { get; set; }

        public IEnumerable<Group> groupsCredit { get; set; }

        public IEnumerable<Room> roomsAvailable { get; set; }

        public int success { get; set; }
    }
}